package com.example.project.service.impl;

import com.example.project.common.MyTool;
import com.example.project.dao.SafetyInspeDao;
import com.example.project.dto.SafetyInspeBinPageDTO;
import com.example.project.dto.SafetyTrainBinDTO;
import com.example.project.entity.SafetyInspe;
import com.example.project.entity.SafetyInspeBin;
import com.example.project.dao.SafetyInspeBinDao;
import com.example.project.entity.SafetyTrainBin;
import com.example.project.service.SafetyInspeBinService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (SafetyInspeBin)表服务实现类
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@Service("safetyInspeBinService")
public class SafetyInspeBinServiceImpl implements SafetyInspeBinService {
    @Resource
    private SafetyInspeBinDao safetyInspeBinDao;
    @Resource
    private SafetyInspeDao safetyInspeDao;

    private MyTool myTool = new MyTool();
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SafetyInspeBin queryById(Integer id) {
        return this.safetyInspeBinDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param safetyInspe 筛选条件
     * @param pageRequest 分页对象
     * @param startTime
     * @param endTime
     * @return 查询结果
     */
    @Override
    public Page<SafetyInspeBinPageDTO> queryByPage(SafetyInspe safetyInspe, PageRequest pageRequest, Date startTime, Date endTime) {
        //删除过去的数据
        deleteAllOldTime();

        long total = this.safetyInspeDao.count(safetyInspe);
        List<SafetyInspeBinPageDTO> safetyInspeBinPageDTOS = new ArrayList<>();

        safetyInspe.setEntRecBin(2);
        List<SafetyInspe> safetyInspeBinList  =  this.safetyInspeDao.queryAllByLimit2(safetyInspe, pageRequest, startTime, endTime);

        for (SafetyInspe e:safetyInspeBinList){
            SafetyInspeBinPageDTO dto = new SafetyInspeBinPageDTO();
            dto.setSafetyInspe(e);
            dto.setSafetyInspeBin(this.safetyInspeBinDao.queryBySafetyInspeId(e.getSafetyInspeId()));
            safetyInspeBinPageDTOS.add(dto);
        }

        return new PageImpl<>(safetyInspeBinPageDTOS, pageRequest, total);
    }

  /**
   *  删除所有到期的时间
   */
    private void deleteAllOldTime() {
        List<SafetyInspeBin> list = this.safetyInspeBinDao.queryAll();
        for (SafetyInspeBin e:list){
            Date date = myTool.dateSomeNOw(e.getBinTime(),e.getDeleteTime());
            Date date1 = myTool.dateNow();
            if (date1.before(date)) {
                //"date1 在 date 之后"

            } else if (date1.after(date)) {
                //"date1 在 date 之前"
                this.deleteById1(e.getId());
            } else {
                this.deleteById1(e.getId());
                //"date1 和 date 相等"
            }
        }
    }
    /**
     * 新增数据
     *
     * @param safetyInspeBin 实例对象
     * @return 实例对象
     */
    @Override
    public SafetyInspeBin insert(SafetyInspeBin safetyInspeBin) {
        safetyInspeBin.setDeleteTime(myTool.dateNow());
        this.safetyInspeBinDao.insert(safetyInspeBin);
        return safetyInspeBin;
    }

    /**
     * 修改数据
     *
     * @param safetyInspeBin 实例对象
     * @return 实例对象
     */
    @Override
    public SafetyInspeBin update(SafetyInspeBin safetyInspeBin) {
        this.safetyInspeBinDao.update(safetyInspeBin);
        return this.queryById(safetyInspeBin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.safetyInspeBinDao.deleteById(id) > 0;
    }

    @Override
    public boolean update1(SafetyInspeBin safetyInspeBin) {
        //修改管理表中的内容
        SafetyInspe safetyInspe  = this.safetyInspeDao.queryById(safetyInspeBin.getSafetyInspeId());
        safetyInspe.setEntRecBin(1);
        this.safetyInspeDao.update(safetyInspe);
        //彻底删除
        return this.deleteById(safetyInspeBin.getId());
    }
    /**
     * 实现彻底删除
     */
    @Override
    public boolean deleteById1(Integer id) {
        //删除管理表中的数据
        SafetyInspeBin safetyInspeBin = this.safetyInspeBinDao.queryById(id);
        this.safetyInspeDao.deleteById(safetyInspeBin.getSafetyInspeId());
        return this.deleteById(id);
    }

    @Override
    public Boolean deleteById2(SafetyTrainBinDTO data) {
        boolean flog1 = false;
        boolean flog2 = false;
        if (data.getBinIds() != null){
            List<Integer> listSTB = myTool.myToList(data.getBinIds());
            flog1 = this.safetyInspeBinDao.deleteByIdAll(listSTB)>0;
        }

        if (data.getIds() != null){
            List<Integer> listST = myTool.myToList(data.getIds());
            flog2 = this.safetyInspeDao.deleteByIdAll(listST)>0;
        }
        return flog2&&flog1;
    }

    @Override
    public Boolean update2(SafetyTrainBinDTO data) {
        boolean flog1 = false;
        boolean flog2 = false;
        if (data.getBinIds() != null){
            List<Integer> listSTB = myTool.myToList(data.getBinIds());
            flog1 = this.safetyInspeBinDao.deleteByIdAll(listSTB)>0;
        }

        if (data.getIds() != null){
            List<Integer> listST = myTool.myToList(data.getIds());
            flog2 = this.safetyInspeDao.batchUpdate(listST)>0;
        }
        return flog2&&flog1;
    }
}
